clear
set more off
cap log close

/*
02_Job_Transitions

This file takes the matched CPS data and calculates 
job transitions under different assumptions, as outlined
in the appendix.

Nov 2023 - created
Feb 2024 - add option for sample restrictions


*/


if "`2'" == "YoungMen"  local 1 "female == 0"
if "`2'" == "OldMen"    local 1 "female == 0"
if "`2'" == "LowMen"    local 1 "female == 0"
if "`2'" == "HighMen"   local 1 "female == 0"


//*****************************
// 
//	General Case
//
//*****************************

//
// Industry-to-industry & union <-> non-union
//

*local v $trans_data

use ${wd}/ipums_ind_trans/matched_cps.dta, clear

* shouldnt be needed anymore
drop if detind_1 == .
drop if detind_2 == .
drop if union_1 == . | union_2 == .

if (regexm("`2'", "EF_")) {
	
	gen     yw = (1 - (abs(27.5 - age) / 20)^2) if abs(27.5 - age) < 20
	replace yw = 0 if missing(yw)
	
	gen young_low  = yw * (.6 * (educ == 3) + .7 * (educ == 1) + 1 * (educ == 2))
	gen young_high = yw * (.4 * (educ == 3) + 1 * (educ == 4))
	
	gen     ow = (1 - (abs(45 - age) / 20)^2) if abs(45 - age) < 20
	replace ow = 0 if missing(ow)
	
	gen old_low  = ow * (.6 * (educ == 3) + .7 * (educ == 1) + 1 * (educ == 2))
	gen old_high = ow * (.4 * (educ == 3) + 1 * (educ == 4))
	
	local x = subinstr("`2'", "EF_", "", .)
    
	replace wgt_1 = wgt_1  * `x'
	keep if !missing(wgt_1)
	keep if wgt_1 > 0
} 

else {
	keep if `1'
}

// transitions - counting
collapse (sum) wgt_1, by(year2 detind_1 union_1 detind_2 union_2)

*replace wgt_1 = 0 if wgt_1 < `cell'

// Rectangularize data
fillin year detind_1 detind_2 union_1 union_2

// jobs
egen job = group(detind_1 union_1)
egen job_2 = group(detind_2 union_2)

// total number of people in each year-industry
egen tot = sum(wgt_1) , by(year2 detind_1 union_1)

// transitions in [0,1] by proportion of moves from j to k industry (including missing?)
gen phi_ii = wgt_1 / tot

replace phi_ii = 0 if phi_ii == .
* set phi_tt to 1 for ease
gen phi_tt = 1
drop _fillin

rename ${ind}_1 $ind
rename union_1 union

// saving
save  ${wd}/ipums_ind_trans/transitions_`2'_case_0.dta, replace



//*****************************
// 
// Restricted case - For decomposition
//
//*****************************


use ${wd}/ipums_ind_trans/matched_cps.dta, clear

* shouldnt be needed anymore
drop if detind_1 == .
drop if detind_2 == .
drop if union_1 == . | union_2 == .
	

if (regexm("`2'", "EF_")) {
	
	gen     yw = (1 - (abs(27.5 - age) / 20)^2) if abs(27.5 - age) < 20
	replace yw = 0 if missing(yw)
	
	gen young_low  = yw * (.6 * (educ == 3) + .7 * (educ == 1) + 1 * (educ == 2))
	gen young_high = yw * (.4 * (educ == 3) + 1 * (educ == 4))
	
	gen     ow = (1 - (abs(45 - age) / 20)^2) if abs(45 - age) < 20
	replace ow = 0 if missing(ow)
	
	gen old_low  = ow * (.6 * (educ == 3) + .7 * (educ == 1) + 1 * (educ == 2))
	gen old_high = ow * (.4 * (educ == 3) + 1 * (educ == 4))
	
local x = subinstr("`2'", "EF_", "", .)
    
	replace wgt_1 = wgt_1  * `x'
	keep if !missing(wgt_1)
	keep if wgt_1 > 0
}

else {
	keep if `1'
}


// transitions - counting
collapse (sum) wgt_1, by(year2 detind_1 union_1 detind_2 union_2)

*replace wgt_1 = 0 if wgt_1 < `cell'

// Rectangularize data
fillin year detind_1 detind_2 union_1 union_2

// jobs
egen job = group(detind_1 union_1)
egen job_2 = group(detind_2 union_2)

// Industry to industry transitions 
egen x  = sum(wgt_1), by( year2 detind_1 detind_2 )
egen x2 = sum(wgt_1), by( year2 detind_1 )

gen phi_ii = x / x2
replace phi_ii = 0 if missing(phi_ii)

drop x x2
// union <----> non_union
egen x  = sum(wgt_1), by( year2 detind_1 union_1  union_2 )
egen x2 = sum(wgt_1), by( year2 detind_1 union_1 )

gen phi_tt = x / x2
replace phi_tt = 0 if missing(phi_tt)


keep year job* $ind* union* phi*
order year job* $ind* union* phi*

rename ${ind}_1 $ind
rename union_1 union

// saving
save  ${wd}/ipums_ind_trans/transitions_`2'_case_6.dta, replace



